package com.app.controller;

import com.app.model.AdminUserModel;
import com.app.model.UpdateMaModel;
import com.app.service.AdminUserService;
import com.app.service.UpdateMaService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.RequestContext;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Controller
public class UpdateMaController {
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private UpdateMaService updateMaService;

    @PostMapping("/insertUpdateMa")//    新增/修改系统信息设置
    public Result<Object> insertRole(
            @RequestParam("id") Integer id,
            @RequestParam("siteName") String siteName,
            @RequestParam("siteIco") String siteIco,
            @RequestParam("siteVersion") String siteVersion,
            HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        UpdateMaModel model = new UpdateMaModel();
        model.setSiteName(siteName);
        model.setSiteIco(siteIco);
        model.setSiteVersion(siteVersion);
        try{
            if(id > 0){
                model.setId(id);
                model.setUpdateTime(CommonHelp.getNowTimestamp());
                updateMaService.update(model);
            }else {
                model.setCreateTime(CommonHelp.getNowTimestamp());
                updateMaService.insert(model);
            }
        }catch (Exception e){
            System.out.println(e);
        }
        return Result.success(null);
    }

//post方法
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 针对post请求，设置允许接收中文
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        try {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setHeaderEncoding("UTF-8");
            List items = upload.parseRequest((RequestContext) request);
            Map params = new HashMap();
            for(Object object:items){
                FileItem fileItem = (FileItem) object;
                if (fileItem.isFormField()) {
                    //这里就相当与FormData中的key,value
                    params.put(fileItem.getFieldName(), fileItem.getString("utf-8"));//如果你页面编码是utf-8的
                }
            }
            //使用params.get获取参数值 send_time就是key
            String val = (String) params.get("key");

        } catch (FileUploadException e1) {
            e1.printStackTrace();
        }
    }
//    @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
//    public String uploadFile(HttpServletRequest request, HttpServletRequest response, HttpSession session) {
//        MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request;
//        MultipartFile multipartFile = multipartRequest.getFile("file1");//file是form-data中二进制字段对应的name
//        System.out.println(multipartFile.getSize());
//        return " ";
//    }
}
